<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<input type="text" id="numId" />
		<input type="button" value="阶乘" onclick="cal()" />
		=
		<span id="resultId"></span>

		<script type="text/javascript">
			function cal() {
				var num = document.getElementById("numId").value;
				// 算计阶乘，考虑一下如果输入的不是数字isNaN()

				if (num == "") {
					var resultSpan = document.getElementById("resultId");
					resultSpan.innerHTML = "不能为空";
					return;
				}

				if (isNaN(num)) {

					var resultSpan = document.getElementById("resultId");
					var result = "输入的不是数字";
					resultSpan.innerHTML = result;
					return;
				} else if (num < 0) {
					console.log("负数没有阶乘");
					var resultSpan = document.getElementById("resultId");
					var result = "负数没有阶乘";
					resultSpan.innerHTML = result;
				} else if (num == 0) {
					var resultSpan = document.getElementById("resultId");
					var result = 1;
					resultSpan.innerHTML = result;
				} else {
					var resultSpan = document.getElementById("resultId");
					var n = parseInt(num);
					console.log(n);
					var sum = 1;
					for (var i = 1; i <= n; i++) {
						sum = sum * i;

					}
					console.log(sum);
					resultSpan.innerHTML = sum;




				}

				// var result = 234;
				// var resultSpan = document.getElementById("resultId");
				// resultSpan.innerHTML = result;

			}
		</script>
	</body>
</html>
